package com.cqtczh.qhd.driver.acticity.carteam;

import httptools.HttpJsonTool;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tools.L;

import javabeen.CarTeamAddDriverInfoBeen;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.InputFilter;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;
import com.cqtczh.qhd.driver.AppContent;
import com.cqtczh.qhd.driver.R;
import com.cqtczh.qhd.driver.acticity.base.BaseActivity;
import com.cqtczh.qhd.driver.api.GagApi;

public class CarTeamAddDriver extends BaseActivity {
	private int CarTeamId;
	private Button addDriver;
	private Button UpLoadRequest;
	private TextView titleTextView;
	/**
	 * 
	 */
	private List<CarTeamAddDriverInfoBeen> carTeamAddDriverInfoBeens;
	private AppContent app;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_car_team_add_driver);
		Ini();
	}

	private void Ini() {
		app = (AppContent) getApplication();
		CarTeamId = Integer.parseInt(getIntent().getStringExtra("CarTeamId"));
		addDriver = (Button) findViewById(R.id.add_Driver_Edit);
		titleTextView = (TextView) findViewById(R.id.head_title);
		titleTextView.setText("邀请司机");
		ImageView imageView=(ImageView) findViewById(R.id.head_left);
		imageView.setVisibility(View.VISIBLE);
		imageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				CarTeamAddDriver.this.finish();
			}
		});
		UpLoadRequest = (Button) findViewById(R.id.sendmessage);
		addDriver.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				AddView();
			}
		});
		UpLoadRequest.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				UpLoadData();
			}
		});
	}

	private void AddView() {
		LinearLayout driverphoneLinearLayout = (LinearLayout) findViewById(R.id.DriverPhone);
		EditText newEditText = new EditText(this);
		newEditText.setHint("请输入司机的电话号码");
		newEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
		newEditText
				.setFilters(new InputFilter[] { new InputFilter.LengthFilter(11) });
		driverphoneLinearLayout.addView(newEditText);

	}

	private void removeView() {
		LinearLayout driveraddmessagelLayout = (LinearLayout) findViewById(R.id.DriverRespon);
		LinearLayout driverphoneLinearLayout = (LinearLayout) findViewById(R.id.DriverPhone);
		driverphoneLinearLayout.removeAllViews();
		EditText newEditText = new EditText(this);

		newEditText.setHint("请输入司机的电话号码");
		newEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
		newEditText
				.setFilters(new InputFilter[] { new InputFilter.LengthFilter(11) });
		driverphoneLinearLayout.addView(newEditText);

		driveraddmessagelLayout.removeAllViews();
		if (carTeamAddDriverInfoBeens != null) {

			int courrn = carTeamAddDriverInfoBeens.size();
			for (int j = 0; j < courrn; j++) {
				TextView textView = new TextView(this);
				if (carTeamAddDriverInfoBeens.get(j).getError().equals("已发送邀请码！")) {
					textView.setText("司机："
							+ carTeamAddDriverInfoBeens.get(j).getPhone() + "不是我们平台司机，"
							 + carTeamAddDriverInfoBeens.get(j).getError()+"请他加入我们平台。");
				}else if(carTeamAddDriverInfoBeens.get(j).getError().equals("邀请好友成功！")) {
					textView.setText("司机："
							+ carTeamAddDriverInfoBeens.get(j).getPhone() + "已经向他发送邀请。");
				}else if(carTeamAddDriverInfoBeens.get(j).getError().equals("邀请好友失败！")){
					textView.setText("司机："
							+ carTeamAddDriverInfoBeens.get(j).getPhone() + "添加失败；"
							+ "原因：该司机还未登录手机APP" );
				}else if(carTeamAddDriverInfoBeens.get(j).getError().equals("您输入司机已经加入车队,请重新输入!")){
					textView.setText("司机："
							+ carTeamAddDriverInfoBeens.get(j).getPhone() + "添加失败；"
							+ "原因：您输入司机已经加入车队,请重新输入!" );
				}
				else {				
				textView.setText("司机："
						+ carTeamAddDriverInfoBeens.get(j).getPhone() + "添加失败；"
						+ "原因：该司机审核未通过" );
				}
				driveraddmessagelLayout.addView(textView);
			}
		}
	}

	private void UpLoadData() {
		List<String> DATA = getViewVaule();
		if (DATA.size() <= 0) {
			return;
		}
		closeInput();
		StartUp(DATA);
	}

	private void closeInput() {
		InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		if (inputMethodManager!=null) {
			IBinder  mIBinder=this.getCurrentFocus().getWindowToken();
			if (mIBinder!=null) {
				inputMethodManager.hideSoftInputFromWindow(mIBinder, InputMethodManager.HIDE_NOT_ALWAYS);
			}
		
		}
		
	}

	private void StartUp(List<String> phoneno) {
		HttpJsonTool httpJsonTool = new HttpJsonTool(this);
		httpJsonTool.SetOnLister(new Handler() {
			@SuppressWarnings("unchecked")
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what == 1) {

					carTeamAddDriverInfoBeens = (List<CarTeamAddDriverInfoBeen>) msg.obj;
					if (carTeamAddDriverInfoBeens.size() > 0) {
						removeView();
					}else {
						Toast.makeText(CarTeamAddDriver.this, "邀请成功", Toast.LENGTH_LONG).show();
					}
				}
			}
		});
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("phone", phoneno);
		data.put("carownerid", app.userId);
		Type type=new TypeToken<List<CarTeamAddDriverInfoBeen>>(){}.getType();
		httpJsonTool.StartWork(data, GagApi.CAR_TEAM_ADD_DRIVER, type, null);
	}

	private List<String> getViewVaule() {
		List<String> vaule = new ArrayList<String>();
		LinearLayout driverphoneLinearLayout = (LinearLayout) findViewById(R.id.DriverPhone);
		for (int i = 0; i < driverphoneLinearLayout.getChildCount(); i++) {
			EditText vi = (EditText) driverphoneLinearLayout.getChildAt(i);
			String str = vi.getText().toString();
			if (str.length() > 0) {
				vaule.add(str);
			}
		}
		return vaule;
	}

}
